package com.buddy.design.pattern._03proxy.jdk;

import com.buddy.design.pattern._03proxy.Person;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class Meipo implements InvocationHandler {

    private Person target;

    //获取被代理人的个人资料
    //返回被代理对象
    public Object getInstance(Person target) {
        this.target = target;
        Class clazz = target.getClass();
        //类加载器，接口，代理人
        System.out.println(">>>>>>被代理对象是"+clazz);
        //TODO
        /*
         * if (interfaces.length > 65535) {
         *  throw new IllegalArgumentException("interface limit exceeded");
         *  }
         */
        return Proxy.newProxyInstance(clazz.getClassLoader(), clazz.getInterfaces(), this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //findlove之前需要做的视频
        System.out.println("调用目标对象方法之前需要做的事情");
        this.target.findLove();
        System.out.println("调用目标对象方法之后需要做的事情");
        return null;
    }
}
